var Rson = require('Rson');

cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if(cc.webSocket != null)
        {
            // 更改响应回调函数
            cc.webSocket.onmessage = this.onWSMsg;
            cc.webSocket.onclose = this.onWSClose;
        }
    },

    // start () {

    // },

    // 战斗时websocket回调
    onWSMsg: function(event){
        var data = Rson.decode(event.data);
        var msg = data.data;
        cc.log("serversend: code: " + data.code + ' data: ' + JSON.stringify(msg) + ' to: ' + data.to);

        switch(data.code){
            case '21':
                // 游戏开始
                cc.eventCenter.GameStart.broadcast(data, msg);
            break;

            case '23':
                // 回合开始
                cc.eventCenter.TurnStart.broadcast(data, msg);
            break;

            case '24':
                // 回合结束
                cc.eventCenter.TurnEnd.broadcast(data, msg);
            break;

            case '31':
                // 超速回线 反馈
                cc.eventCenter.LBCheck.broadcast(data, msg);
            break;

            case '33':
                // 超速回线 确认
                cc.eventCenter.LBDone.broadcast(data, msg);
            break;

            case '41':

                // 防火墙 反馈
                cc.eventCenter.FWCheck.broadcast(data, msg);
            break;

            case '43':
                // 防火墙 确认
                cc.eventCenter.FWDone.broadcast(data, msg);
            break;

            case '51':
                // 探查 反馈
                cc.eventCenter.VCCheck.broadcast(data, msg);
            break;

            case '53':
                // 探查 结束
                cc.eventCenter.VCDone.broadcast(data, msg);
            break;

            case '61':
                // 交换 反馈
                cc.eventCenter.NFCheck.broadcast(data, msg);
            break;

            case '63':
                // 交换 确认
                cc.eventCenter.NFDone.broadcast(data, msg);
            break;

            case '201':
                //随机验证 反馈
                cc.eventCenter.RVCheck.broadcast(data, msg);
            break;

            case '203':
                //随机验证 确认
                cc.eventCenter.RVDone.broadcast(data, msg);
            break;

            case '210':
                // 零日攻击 反馈
                cc.eventCenter.ZDACheck.broadcast(data, msg);
            break;

            case '211':
                //零日攻击 确认
                cc.eventCenter.ZDADone.broadcast(data, msg);
            break;

            case '220':
                // 蠕虫扩散 反馈
                cc.eventCenter.WDCheck.broadcast(data, msg);
            break;

            case '221':
                // 蠕虫扩散 确认
                cc.eventCenter.WDDone.broadcast(data, msg);
            break;

            case '230':
                //沙盒测试 反馈
                cc.eventCenter.SBCheck.broadcast(data, msg);
                break;

            case '231':
                //沙盒测试 确认
                cc.eventCenter.SBDone.broadcast(data, msg);
                break;

            case '71':
                // 棋子选择 反馈
                cc.eventCenter.SelectMove.broadcast(data, msg);
            break;

            case '73':
                cc.eventCenter.MoveDone.broadcast(data, msg);
            break;

            case '81':
                // 弹幕
                cc.eventCenter.ShotBullet.broadcast(data, msg);
            break;

            case '91':
                // 游戏结束
                cc.eventCenter.GameFinish.broadcast(data, msg);
            break;
            
            case '106':
                // 接收地图信息
                cc.eventCenter.RecvMap.broadcast(data, msg);
            break;
        }
    },

    // 战斗场景ws关闭回调
    onWSClose: function(event){

    },
});
